package com.wwp.wkb.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ui.ActivityManagerUtils;
import com.blankj.utilcode.util.ui.StatusBarUtils;
import com.wwp.wkb.R;
import com.wwp.wkb.base.impl.BaseViewDefault;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity implements BaseView {
    public Activity context;
    public BaseView baseView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ActivityManagerUtils.getIntence().addActivity(this);
        ButterKnife.bind(this);
        context = this;
        baseView = createLoading(this);
        setBack();
        setUpView();
    }

    protected abstract int getLayoutId();

    protected abstract void setUpView();

    protected BaseView createLoading(Context context) {
        return new BaseViewDefault(context);
    }

    public void setStatusBarLightMode() {
        StatusBarUtils.setSystemUiVisibility(this.getWindow());
        StatusBarUtils.MIUISetStatusBarLightMode(this.getWindow(), true);
        StatusBarUtils.FlymeSetStatusBarLightMode(this.getWindow(), true);
    }

    public void setFitsSystemWindows() {
        StatusBarUtils.setFitsSystemWindows(this.getWindow());
    }

    public void setBack() {
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        if (ivBack != null) {
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    public void setBackGone() {
        ImageView iv_back = (ImageView) findViewById(R.id.iv_back);
        if (iv_back != null) {
            iv_back.setVisibility(View.GONE);
        }
    }

    public void setTitleBarTitle(String title) {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    public void setTitleBarTitle(int title) {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManagerUtils.getIntence().removeActivity(this);
    }

    @Override
    public void showToast(String msg) {
        baseView.showToast(msg);
    }

    @Override
    public void showLoading() {
        baseView.showLoading();
    }

    @Override
    public void showLoading(String msg) {
        baseView.showLoading(msg);
    }

    @Override
    public void disLoading() {
        baseView.disLoading();
    }
}